package com.tsystems.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.tsystems.entities.Station;
import com.tsystems.entities.TimeTableElement;
import com.tsystems.entities.Train;

public class TimeTableElementDAO extends AbstractDAO<TimeTableElement> {

	public TimeTableElement getEqualsTimeTableElement(
			TimeTableElement timeTableElement) {

		EntityManager em = TrainsEntityManager.getInstance();
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<TimeTableElement> criteria = builder
				.createQuery(TimeTableElement.class);

		Root<TimeTableElement> timeTableElements = criteria
				.from(TimeTableElement.class);
		criteria.select(timeTableElements);
		criteria.where(builder.and(builder.equal(
				timeTableElements.get("train"), timeTableElement.getTrain()),
				builder.equal(timeTableElements.get("departureStation"),
						timeTableElement.getDepartureStation()), builder.equal(
						timeTableElements.get("departureTime"),
						timeTableElement.getDepartureTime())));
		List<TimeTableElement> result = em.createQuery(criteria)
				.getResultList();

		return !result.isEmpty() ? result.get(0) : null;
	}
	public List<TimeTableElement> getTimeTableElementByArrivalInformation(
			Train train, Station arrivalStation, Date arrivalTime) {

		EntityManager em = TrainsEntityManager.getInstance();
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<TimeTableElement> criteria = builder
				.createQuery(TimeTableElement.class);

		Root<TimeTableElement> timeTableElements = criteria
				.from(TimeTableElement.class);
		criteria.select(timeTableElements);
		criteria.where(builder.and(builder.equal(
				timeTableElements.get("train"), train),
				builder.equal(timeTableElements.get("arrivalStation"),
						arrivalStation), builder.equal(
						timeTableElements.get("arrivalTime"),
						arrivalTime)));
		List<TimeTableElement> result = em.createQuery(criteria)
				.getResultList();

		return result;
	}
	
	public TimeTableElement getTimeTableElementByDepartureInformation(
			Train train, Station departureStation, Date departureTime) {

		EntityManager em = TrainsEntityManager.getInstance();
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<TimeTableElement> criteria = builder
				.createQuery(TimeTableElement.class);

		Root<TimeTableElement> timeTableElements = criteria
				.from(TimeTableElement.class);
		criteria.select(timeTableElements);
		criteria.where(builder.and(builder.equal(
				timeTableElements.get("train"), train),
				builder.equal(timeTableElements.get("departureStation"),
						departureStation), builder.equal(
						timeTableElements.get("departureTime"),
						departureTime)));
		List<TimeTableElement> result = em.createQuery(criteria)
				.getResultList();

		return !result.isEmpty() ? result.get(0) : null;
	}

	public List<TimeTableElement> getTimeTableElementBase() {

		EntityManager em = TrainsEntityManager.getInstance();
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<TimeTableElement> criteria = builder
				.createQuery(TimeTableElement.class);

		Root<TimeTableElement> timeTableElements = criteria
				.from(TimeTableElement.class);
		criteria.select(timeTableElements);
		List<TimeTableElement> result = em.createQuery(criteria)
				.getResultList();

		return  result;
	}

	public List<TimeTableElement> getTimeTableElementsByArrivalStation(
			Station arrivalStation) {

		EntityManager em = TrainsEntityManager.getInstance();
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<TimeTableElement> criteria = builder
				.createQuery(TimeTableElement.class);

		Root<TimeTableElement> timeTableElements = criteria
				.from(TimeTableElement.class);
		criteria.select(timeTableElements);
		criteria.where(builder.equal(timeTableElements.get("arrivalStation"),
				arrivalStation));
		List<TimeTableElement> result = em.createQuery(criteria)
				.getResultList();

		return result;
	}

	public List<TimeTableElement> getTimeTableElementsByDepartureStation(
			Station departureStation) {

		EntityManager em = TrainsEntityManager.getInstance();
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<TimeTableElement> criteria = builder
				.createQuery(TimeTableElement.class);

		Root<TimeTableElement> timeTableElements = criteria
				.from(TimeTableElement.class);
		criteria.select(timeTableElements);
		criteria.where(builder.equal(timeTableElements.get("departureStation"),
				departureStation));
		List<TimeTableElement> result = em.createQuery(criteria)
				.getResultList();

		return result;
	}
	
	public List<TimeTableElement> getTimeTableElementsByTrain(
			Train train) {

		EntityManager em = TrainsEntityManager.getInstance();
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<TimeTableElement> criteria = builder
				.createQuery(TimeTableElement.class);

		Root<TimeTableElement> timeTableElements = criteria
				.from(TimeTableElement.class);
		criteria.select(timeTableElements);
		criteria.where(builder.equal(timeTableElements.get("train"),
				train));
		List<TimeTableElement> result = em.createQuery(criteria)
				.getResultList();

		return result;
	}
}
